package com.ruijie.acl.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.AclPlanParameters;
import com.ruijie.acl.service.AccessGroupService;
import com.ruijie.acl.service.AceService;
import com.ruijie.acl.service.AclPlanParametersService;
import com.ruijie.acl.task.config.TaskConfigAclDevice;
import com.ruijie.acl.task.model.AccessGroupInfo;
import com.ruijie.acl.type.AclPlanTypeEnum;
import com.ruijie.acl.utils.AclConfigUtil;
import com.ruijie.acl.utils.AclUtil;
import com.ruijie.acl.web.config.Constants;
import com.ruijie.batch.core.factory.SpringConfigAware;
import com.ruijie.batch.plan.PlanParameters;
import com.ruijie.batch.plan.items.ItemParameters;
import com.ruijie.batch.task.ItemProcessResult;
import com.ruijie.emp.batch.task.AbstractDeviceExtendTasklet;
import com.ruijie.emp.system.service.TelnetTemplateService;

/**
 * 
 * <p>
 * Title: 删除时间段,ACL部署计划
 * </p>
 * <p>
 * Description: Function Description
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-09-27
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * 
 * @author ASAI
 */
public class DeleteTimeRangeTask extends AbstractDeviceExtendTasklet implements SpringConfigAware {

	private Log logger = LogFactory.getLog(DeleteTimeRangeTask.class);

	private TelnetTemplateService service;

	private AclPlanParametersService aclPlanParametersService;

	private AccessGroupService accessGroupService;

	private AceService aceService;

	private TaskConfigAclDevice taskConfigAclDevice;

	private Map<String, String> paramMap = new HashMap<String, String>();

	protected Map<String, String> parseParameters(ItemParameters itemParameters) {
		return paramMap;
	}

	@SuppressWarnings("unchecked")
	public ItemProcessResult processItem(Object item, ItemParameters itemParameters) {
		ItemProcessResult result = new ItemProcessResult();
		paramMap.clear();
		Assert.notNull(item, "item is null");

		PlanParameters planParameters = new PlanParameters();

		// 取得参数
		String aclPlanParamtersId = itemParameters.getParameterValue(Constants.ACL_DEPLOY_PLAN_ITEM_PARAM_NAME);
		AclPlanParameters aclPlanParamters = aclPlanParametersService.get(Long.parseLong(aclPlanParamtersId));

		logger.debug("参数ID：" + aclPlanParamters.getId().toString() + "时间段：" + aclPlanParamters.getTimeRanges());
		String timeRangeNames = aclPlanParamters.getTimeRanges();
		// 取得要删除的时间段信息
		if (!AclUtil.empty(timeRangeNames)) {
			// timeRangeNames = AclUtil.unescapeSpecialString(timeRangeNames);
			planParameters.putParameter("timerangeInfoList", AclConfigUtil.string2TimeRangeInfoList(timeRangeNames));
			this.paramMap.put(Constants.ACL_DEPLOY_PLAN_ITEM_PARAM_NAME_TIMENAMES, AclUtil
					.unescapeSpecialString(timeRangeNames));

			// 取得与时间段相关的组信息
			if (!AclUtil.empty(aclPlanParamters.getAccessGroups())) {
				List<AccessGroup> accessGroupList = accessGroupService.findByIds(aclPlanParamters.getAccessGroups());
				if (accessGroupList != null && accessGroupList.size() > 0) {
					List<AccessGroupInfo> accessGroupInfoList = aceService.accessGroup2AccessGroupInfo(accessGroupList);
					this.paramMap.put(Constants.ACL_DEPLOY_PLAN_ITEM_PARAM_NAME_GROUPNAMES, AclConfigUtil
							.convertAccessGroupNames(accessGroupInfoList));
					// this.removeAccessGroup4TimeRange(accessGroupInfoList,timeRangeInfoList);
					planParameters.putParameter("accessGroupInfoList", accessGroupInfoList);
				}
			}
			// 调用下发
			planParameters.putParameter("dealFlag", AclPlanTypeEnum.DeleteTimeRange.toString());
			
			return taskConfigAclDevice.processItem(item, planParameters);
		} else {
			logger.warn("要删除的时间段信息列表为空！");
		}

		return result;
	}

	public void setService(TelnetTemplateService service) {
		this.service = service;
	}

	public AclPlanParametersService getAclPlanParametersService() {
		return aclPlanParametersService;
	}

	public void setAclPlanParametersService(AclPlanParametersService aclPlanParametersService) {
		this.aclPlanParametersService = aclPlanParametersService;
	}

	public AccessGroupService getAccessGroupService() {
		return accessGroupService;
	}

	public void setAccessGroupService(AccessGroupService accessGroupService) {
		this.accessGroupService = accessGroupService;
	}

	public AceService getAceService() {
		return aceService;
	}

	public void setAceService(AceService aceService) {
		this.aceService = aceService;
	}

	public TaskConfigAclDevice getTaskConfigAclDevice() {
		return taskConfigAclDevice;
	}

	public void setTaskConfigAclDevice(TaskConfigAclDevice taskConfigAclDevice) {
		this.taskConfigAclDevice = taskConfigAclDevice;
	}

	public TelnetTemplateService getService() {
		return service;
	}
}
